# -*- coding: iso-8859-2 -*-

import os
import urllib2
import re
import traceback

from urllib2 import URLError

from authorshipanalyzer.file import Path

class ArchiveDownloader:
    '''
    Download archive files with info1 archives from
    http page
    @param archiveAddress
    '''
    
    def __init__(self, archiveAddress = 'http://home.agh.edu.pl/Lists/info1/'):
        '''
        @param archiveAddress: address of http page witch
        info1 archive;
        default: http://home.agh.edu.pl/Lists/info1/
        '''
        self.archiveAddress = archiveAddress
    
    def update(self):
        '''
        Update info1 archive
        '''
        info1Dir = Path.getInfo1Dir()
        info1List = os.listdir(info1Dir)
       
        #update file from latest month
        latest = self._getLatestArchive()
        if latest != None:
            self._download(latest)
       
        info1RemoteList = self._createRemoteArchiveList()
        for info1RemoteFile in info1RemoteList:
            if info1RemoteFile not in info1List:
                self._download(info1RemoteFile)

    
    def _createRemoteArchiveList(self): 
        '''
        Create list of files with month archives
        from info1
        '''  
        result = []
        try:
            response = urllib2.urlopen(self.archiveAddress)
            archiveHtml = response.read()
            tags = archiveHtml.split('<')
            for tag in tags:
                matcher = re.match(".*href=\"([^\"]+)\">.*", tag, re.I)
                if matcher != None:
                    monthArchive = matcher.group(1)
                    if re.match("^info1.\d\d\d\d-\d\d$", monthArchive, re.I):
                        result.append(monthArchive)
        except URLError, ex:
            traceback.print_exc()
            
        return result
    
    def _download(self, archive):
        '''
        Download archive and save it in a local directory
        with info1 files
        @param archive: simple name of an archive file
        '''
        url = self.archiveAddress + archive
        archiveContent = ""
        try:
            response = urllib2.urlopen(url)
            archiveContent = response.read()
        except URLError, ex:
            traceback.print_exc()  
             
        try:
            #save to file
            filePath = Path.getInfo1Dir() + os.sep + archive
            file = open(filePath, 'w')
            file.write(archiveContent)
            file.flush()
            file.close()
        except IOError, ex:
            traceback.print_exc() 

    def _getLatestArchive(self):
        '''
        Return latest local archive.
        If archive directory is empty, return None.
        '''
        info1Dir = Path.getInfo1Dir()
        info1List = os.listdir(info1Dir)
        latest = None
        for file in info1List:
            if latest == None:
                latest = file
            elif file > latest:
                latest = file
        return latest
    
if __name__ == "__main__":
    ArchiveDownloader().update()